window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js', 'imgUpload_module.js', ], function(Z, I, M, U, IMG) {
		var user = U.checkLogin();
		var personVO = new Object();
		var user_typ=U.getParam('typ');
		U.fillHead();
		U.fillFooter();
		initPage();

		function initPage() { //初始化页面
			U.loadOpt(false);
			if(!checklogin()) {
				return false;
			}
			$("body>div").show();
			U.loadOpt(false);
			U.ajaxget("/person/detailByUid/" + user.info.id, {}, function(json) {
				if(json.success) {
					personVO = json.models.personVO;
					if(personVO.status == 2) {
						$(".submint").text("保存修改");
					}
					if(personVO.status == 0) {
						$(".submint").text("提交申请");
					}
					if(personVO.status == 1) {
						location.href = "wait_review.html";
					}
					if(personVO.status == 2 && (U.getParam("type") == null)) {
						location.href = "center.html";
					}
					var divs = $(".content>div>div");
					$(".content select").val(personVO.sex);
					var headImage = "../images/icons/icon_unknow.png";
					if(U.getImg(personVO.headImage).length > 0) {
						headImage = U.getImg(personVO.headImage)[0];
					}
					$(".touxiang>img").attr("src", headImage);
					$(".touxiang>img").attr("uploadimg", headImage);
					var otherImage = "../images/icons/default_card.png";
					if(U.getImg(personVO.otherImage).length > 0) {
						otherImage = U.getImg(personVO.otherImage)[0];
					}
					$(".fj_img>img").attr("src", otherImage);
					$(".fj_img img").attr("uploadimg", otherImage);
					$(divs[0]).find("input").val(personVO.name);
					$(divs[2]).find("input").val(personVO.mobile);
					$(divs[3]).find("input").val(personVO.weixin);
					$(divs[4]).find("input").val(personVO.email);
					$(divs[5]).find("input").val(personVO.cardCode);
					$(divs[8]).find("input").val(personVO.address);
					$(divs[9]).find("input").val(personVO.city);
					$(divs[10]).find("input").val(personVO.requirement);
					$(divs[11]).find("input").val(personVO.helpArea);
					$(divs[12]).find("input").val(personVO.remark);
				} else {
					M.toast(false, json.errorMsg);
				}
			});
			initEvent();
		}

		function initEvent() {
			if(U.getParam("status") == "1") {
				$("input").css("border", "none");
				$("input").attr("readonly", "readonly");
				$("select").attr("disabled", "disabled");
				$(".submint,.upload_fj").hide();
				return false;
			}
			$(".content>div>div input").width($(".content>div").width() - 220);

			$(".content>div>div input").each(function(i) {
				if(i > 4) {
					return false;
				}
				$(".content>div>div input").eq(i).width(($(".content>div").width() - 220) / 2);
			});
			
			
			$("#head_img").width(200);
			$("#fj").width(200);
			
			$(".touxiang>img").click(function() {
				if(!checklogin()) {
					return false;
				}
				$("#head_img").click();
			});
			$(".upload_fj").click(function() {
				if(!checklogin()) {
					return false;
				}
				$("#fj").click();
			});
			$(".submint").click(function() {
				if(!checklogin()) {
					return false;
				}
				var divs = $(".content>div>div");
				var name = $(divs[0]).find("input").val();
				var headImage = $(".touxiang>img").attr("uploadimg");
				var mobile = $(divs[2]).find("input").val();
				var weixin = $(divs[3]).find("input").val();
				var email = $(divs[4]).find("input").val();
				var cardCode = $(divs[5]).find("input").val();
				var address = $(divs[8]).find("input").val();
				var sex = $(".content select").val();
				var cardType = 1;
				var otherImage = $(".fj_img>img").attr("uploadimg");
				var city = $(divs[9]).find("input").val();
				var helpArea = $(divs[10]).find("input").val();
				var requirement = $(divs[11]).find("input").val();
				var remark = $(divs[12]).find("input").val();
				if(mobile == "" || weixin == "" || headImage == "" || otherImage == "" || name == "" || cardCode == "" || email == "" || address == "" || city == "" || requirement == "" || helpArea == "") {
					M.toast(false, "请填写完整信息!");
					return false;
				}
				var status = personVO.status;
				if(status == 0) {
					status = 1;
				}
				var param = {
					mobile: mobile,
					id: personVO.id,
					userId: user.info.id,
					name: name,
					headImage: headImage,
					cardCode: cardCode,
					cardType: cardType,
					otherImage: otherImage,
					email: email,
					weixin: weixin,
					address: address,
					city: city,
					helpArea: helpArea,
					sex: sex,
					requirement: requirement,
					remark: remark,
					status: status
				};
				U.loadOpt(true);
				
				var request='';
				if(user_typ==0){
					request='/person/apply';
				}else{
					request="/person/edit";
				}
				U.ajax(request, param, function(json) {
					U.loadOpt(false);
					if(json.success) {
						if(personVO.status == 2) {
							alert("保存成功!");
							history.back();
						} else {
							alert("申请已提交!");
							location.href = "wait_review.html";
						}
					} else {
						M.toast(false, json.errorMsg);
					}
				});
			});
			if(U.isIE()) {
				$("#head_img").show();
				$(".touxiang img").unbind("click");
//				IMG.ajaxFileUpload("head_img", function(result) {
//					if(result.success) {
//						$(".touxiang img").attr("uploadimg", result.models.url + "");
//						$(".touxiang img").attr("src", result.models.url + "");
//					}
//				});
				$(".upload_fj").hide();
				$("#fj").show();
				$(".upload_fj").unbind("click");
//				IMG.ajaxFileUpload("fj", function(result) {
//					if(result.success) {
//						$(".fj_img img").attr("uploadimg", result.models.url + "");
//						$(".fj_img img").attr("src", result.models.url + "");
//					}
//				});
				registerheadImg();
				registerfj();
			} else {
				IMG.uploadImg({
					'obj': $('#head_img'),
					'success': function(json, obj) {
						U.loadOpt(false);
						if(json.state == "SUCCESS") {
							$(".touxiang img").attr("uploadimg", json.url + "");
							$(".touxiang img").attr("src", json.url + "");
						} else {
							M.toast(false, "上传失败！");
						}
					},
					'error': function(msg) {
						M.error(msg);
					}
				});
				IMG.uploadImg({
					'obj': $('#fj'),
					'success': function(json, obj) {
						U.loadOpt(false);
						if(json.state == "SUCCESS") {
							$(".fj_img img").attr("uploadimg", json.url + "");
							$(".fj_img img").attr("src", json.url + "");

						} else {
							M.toast(false, "上传失败！");
						}
					},
					'error': function(msg) {
						M.error(msg);
					}
				});
			}

		}

		function checklogin() {
			var is_login = true;
			if(user.state != 1) {
				is_login = false;
				alert("请先登录!");
				$("body").empty();
				U.loadLogin(true, false);
			}
			return is_login;
		}

		var count = 1;

		function registerheadImg() {
			IMG.ajaxFileUpload("head_img", function(result) {
				if(result.success) {
					$(".touxiang img").attr("uploadimg", result.models.url + "");
					$(".touxiang img").attr("src", result.models.url + "");
					$("#head_img").replaceWith("<input type='file'  name= 'head_img' id='head_img'>");
					registerheadImg();
				}
			});
		}

		function registerfj() {
			IMG.ajaxFileUpload("fj", function(result) {
				if(result.success) {
					$(".fj_img img").attr("uploadimg", result.models.url + "");
					$(".fj_img img").attr("src", result.models.url + "");
					$("#fj").replaceWith("<input type='file'  name= 'fj' id='fj'>");
					registerfj();
				}
			});
		}
	});
}